package com.itheima.blog.admin.controller;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.api.client.dto.TBannerDto;
import com.itheima.blog.admin.aop.MyLog;
import com.itheima.blog.admin.entity.TBanner;
import com.itheima.blog.admin.service.ITBannerService;
import com.itheima.common.domain.PageReqDto;
import com.itheima.common.domain.PageResDto;
import com.itheima.common.domain.R;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import org.springframework.web.multipart.MultipartFile;

import java.util.List;

/**
 * <p>
 * 轮播图设置表 前端控制器
 * </p>
 *
 * @author blog
 * @since 2023-07-11
 */
@RestController
@RequestMapping("/banner")
@RequiredArgsConstructor
@Api("轮播图管理")
public class TBannerController {

    private final ITBannerService itBannerService;

    @GetMapping("/list")
    @ApiOperation("列表展示")
    public PageResDto<TBannerDto> findPage(PageReqDto pageReqDto){
        return itBannerService.findPage(pageReqDto);
    }

    @GetMapping("/{id}")
    @ApiOperation("根据id查询轮播图")
    public R<TBanner> findBannerById(@PathVariable("id") Long id){
        return itBannerService.findBannerById(id);
    }

    @MyLog(value = "上传轮播图")
    @PostMapping("/upload")
    @ApiOperation("上传轮播图")
    public R uploadBannerPicture(@RequestPart("file") MultipartFile file){
        itBannerService.uploadBannerPicture(file);
        return R.ok();
    }

    @MyLog(value = "保存轮播图")
    @PostMapping("/update/{id}")
    @ApiOperation("裁剪轮播图")
    public R updateBannerPicture(@RequestPart("file") MultipartFile file,@PathVariable("id")Long id){
        itBannerService.updateBannerPicture(file,id);
        return R.ok();
    }

    @MyLog(value = "删除轮播图")
    @DeleteMapping("/delete")
    @ApiOperation("删除轮播图")
    public R deleteBannerById(@RequestParam("ids") List<Long> ids){
        itBannerService.deleteBannerById(ids);
        return R.ok();
    }

}
